﻿//////////////////////////////////////////////////////////////
/////	文件名：xmlhttprequest.js                        /////
/////   作  者：黄纲                                     /////
/////   时  间：2006.11.22                               /////
/////   描  述：对系统数据提交时进行异步验证的客户端脚本 /////
//////////////////////////////////////////////////////////////


//为JavaScript添加Trim()属性
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

//创建异步访问对象
function createXHR() 
{
    var xhr;
    
    try 
    {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(E) 
        {
            xhr = false;
        }
    }

    if (!xhr && typeof XMLHttpRequest != 'undefined') 
    {
        xhr = new XMLHttpRequest();
    }
    
    return xhr;
}

//异步访问处理(模态窗体)
var xhr;
var TextBoxID;
var ReturnTextIDs;
var ModalDialogUrl;
var ModalDialogWidth;
var ModalDialogHeight;
var ModalDialogParam = "";
function sender(ValidateID, textBoxID, returnTextIDs, openUrl, width, height) 
{
    xhr = createXHR();
    TextBoxID = textBoxID;
    ReturnTextIDs = returnTextIDs;
    ModalDialogUrl = openUrl;
    ModalDialogWidth = width;
    ModalDialogHeight = height;
    ModalDialogParam = document.getElementById(textBoxID).value;

    if(xhr)
    {
        xhr.onreadystatechange=callbackFunction;
        
        xhr.open("GET", "../XMLHttpRequest/xmlhttprequest.aspx?setTime="+new Date().getTime()+"&ValidateID="+ValidateID+"&ValidateValue="+ModalDialogParam);
        
        xhr.send(null);
    }
    else
    {
        alert("XMLHttpRequest对象创建失败！");
    }
}

//异步访问处理(返回仓存窗体)
var Store_qty;//仓存id
var Im_no;
var Store_id;

function StoreQty(M_ID, P_ID, H_ID, Q_ID) 
{
    xhr = createXHR();
    
    Store_qty = Q_ID;
    
    if(xhr)
    {
        xhr.onreadystatechange=StoreQtybackFunction;
        
        xhr.open("GET", "../XMLHttpRequest/getStoreQty.aspx?M_ID="+M_ID+"&P_ID="+P_ID+"&H_ID="+H_ID+"&Q_ID="+Q_ID);
        
        xhr.send(null);
    }
    else
    {
        alert("XMLHttpRequest对象创建失败！");
    }
}

//仓存回调函数处理
function StoreQtybackFunction()
{
    if (xhr.readyState == 4) 
    {
        if (xhr.status == 200) 
        {
            var returnValue = xhr.responseText;

            document.getElementById(Store_qty).value = returnValue;
        } 
        else 
        {
            alert("页面异常！");
        }
    }
}


//回调函数处理
function callbackFunction()
{
    if (xhr.readyState == 4) 
    {
        if (xhr.status == 200) 
        {
            var returnValue = xhr.responseText;

            //如果返回-1,则表示输入的值不存在或存在多条模糊匹配
            if(returnValue == "-1")
            {
                openModalDialog(ModalDialogUrl, ModalDialogWidth, ModalDialogHeight, TextBoxID, ReturnTextIDs);
            }
            else if(returnValue != null && returnValue.length > 0)//否则输入的值存在
            {
                ModalDialogParam = "";
                var arrTextBoxIDs = ReturnTextIDs.split("|");
                var arrReturnValues = returnValue.split("|");
                var arrTextBoxIDCount = arrTextBoxIDs.length;
                var arrReturnValueCount = arrReturnValues.length;
                
                if(arrTextBoxIDCount > 0 && arrReturnValueCount > 0)
                {
                    var textID = TextBoxID.substring(0, TextBoxID.indexOf(arrTextBoxIDs[0]));

                    for(var i = 0; i < arrTextBoxIDCount; i++)
                    {
                        try
                        {
                            document.getElementById(textID + arrTextBoxIDs[i] + "_txtBox").value = arrReturnValues[i];
                        }catch(er){}
                    }
                }
                
                document.getElementById(TextBoxID).select();
                document.getElementById(TextBoxID).focus();
            }
        } 
        else 
        {
            alert("页面异常！");
        }
    }
}

//打开模态窗口搜索
function openModalDialog(openUrl, width, height, txtBoxID, returnTextIDs, iflag)
{
    //获得隐藏的值(票据核销主表)
    if(iflag == 2)
    {
        ModalDialogParam = document.getElementById("hidType").value;
    }
    
    //获得隐藏的值(票据核销从表)
    if(iflag == 3)
    {
        ModalDialogParam = document.getElementById("hidChildType").value;
    }
    
    //
    if(iflag == 4)
    {
        ModalDialogParam = document.getElementById("").value;
    }
    
    var retValue = window.showModalDialog(openUrl+'?param='+ ModalDialogParam +'&setTime='+new Date().getTime(),'','dialogWidth:'+width+'px;dialogHeight:'+height+'px;status:no;scroll:no;help:no;');
    
    ModalDialogParam = "";
    if(retValue != null)
    { 
        if(retValue != -1)
        {
            var arrTextBoxIDs = returnTextIDs.split("|");
            var arrReturnValues = retValue.split("|");
            var arrTextBoxIDCount = arrTextBoxIDs.length;
            var arrReturnValueCount = arrReturnValues.length;
            
            if(arrTextBoxIDCount > 0 && arrReturnValueCount > 0)
            {
                var textID = txtBoxID.substring(0, txtBoxID.indexOf(arrTextBoxIDs[0]));

                for(var i = 0; i < arrTextBoxIDCount; i++)
                {
                    try
                    {
                        if(iflag == 1 && i == arrTextBoxIDCount - 1)
                        {
                            document.getElementById(textID + arrTextBoxIDs[i]).value = arrReturnValues[i];
                        }
                        else
                        {
                            document.getElementById(textID + arrTextBoxIDs[i] + "_txtBox").value = arrReturnValues[i];
                        }
                    }catch(er){}
                }
                
                //求仓存
                if (iflag == 0)
                {                    
                    txtId = textID;
                    Im_no = document.getElementById(textID+"ctsbChildAddIm_no_txtBox").value;
                    var Batch_no = document.getElementById(textID+"ctbChildAddBatchNo").value;                    
                    Store_id = document.getElementById('hidStoreId').value;

                    StoreQty(Im_no,Batch_no,Store_id,textID+"ctbChildAddStoreQty_txtBox");
                }
                
                //返回值给下拉框选定项
                if(iflag == 1)
                {
                    var index = document.getElementById(textID + arrTextBoxIDs[arrTextBoxIDCount-1]).value;

                    document.getElementById("dropAdd0").options[index].selected = true;
                }
                
                
            }
        }
        else//用户ID过期
        {
            parent.window.location.reload();
        }
    }
    
    document.getElementById(txtBoxID).select();
    document.getElementById(txtBoxID).focus();
        
}

function ModalDialog(openUrl, width, height)
{
    var rturnValue = window.showModalDialog(openUrl+'&setTime='+new Date().getTime(),'','dialogWidth:'+width+'px;dialogHeight:'+height+'px;status:no;scroll:no;help:no;')+"";

    if(rturnValue.Trim() != "" && rturnValue.Trim().length > 0 && rturnValue.Trim() != "undefined")
    {
        document.getElementById("ibtnRef").click();
    }
    
}


//验证日期输入合法
function ValidateDateTime(DateTime) 
{ 
    var datetime = DateTime.Trim();
    
    if(datetime.length == 0)
    {
        alert("日期不能为空！");
        return false; 
    }
    
    var str;
    if(datetime.indexOf('AM') != -1)
    {
        str = datetime.substring(0, datetime.indexOf('AM')).Trim();
    }
    else if(datetime.indexOf('PM') != -1)
    {
        str = datetime.substring(0, datetime.indexOf('PM')).Trim();
    }
    else
    {
        str = datetime;
    }

    var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
    if(r == null)
    {
        var s = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); 
        if(s == null)
        {
            alert("日期格式不正确！");
            return false;
        }
        
        var d= new Date(s[1], s[3]-1,s[4],s[5],s[6],s[7]); 
        if(d.getFullYear()!=s[1] || (d.getMonth()+1)!=s[3] || d.getDate()!=s[4] || d.getHours()!=s[5] || d.getMinutes()!=s[6] || d.getSeconds()!=s[7])
        {
            alert("日期格式不正确！");
            return false; 
        }
    }
} 

//ADD动作批号改变时查询库存kidy
function AddBatchChange(id,wh_id)
{
    var strid = id.substr(14,2);//取得动态ID标示        
    var Batch_no = document.getElementById("dgrdResult_ctl"+strid+'_ctbChildAddBatchNo').value;   
    StoreQty(Im_no,Batch_no,wh_id,"dgrdResult_ctl"+strid+'_ctbChildAddStoreQty_txtBox');    
}

//UPDATE动作批号改变时查询库存kidy
function UpdateBatchChange(id,wh_id)
{
    var strid = id.substr(14,2);//取得动态ID标示
    var strid1 = id.substr(36,2);//取得动态ID标示                
                  
    var Im_no = document.getElementById("dgrdResult_ctl"+strid+'_dgrdChildResult_ctl'+strid1+'_ctsbChildRs0_txtBox').value;
    var Batch_no = document.getElementById("dgrdResult_ctl"+strid+'_dgrdChildResult_ctl'+strid1+'_ctbChildRsBatchNo').value;    
    StoreQty(Im_no,Batch_no,wh_id,"dgrdResult_ctl"+strid+'_dgrdChildResult_ctl'+strid1+'_ctsbChildRsStoreQty_txtBox');    
}
